package com.xianyun.site.auth.service.execute;

import com.alibaba.cola.dto.Response;
import com.xianyun.common.core.utils.StringUtils;
import com.xianyun.common.redis.service.RedisService;
import com.xianyun.site.auth.common.constant.AuthConstant;
import com.xianyun.site.auth.common.config.system.LoginType;
import com.xianyun.site.auth.common.exception.JustAuthLoginException;
import com.xianyun.site.auth.service.ISystemUserService;
import com.xianyun.site.iblog.api.user.dubbo.SocialUserEchoService;
import com.xianyun.site.iblog.api.user.dubbo.SystemUserEchoService;
import com.xianyun.site.iblog.dto.user.SocialUserAddCmd;
import com.xianyun.site.iblog.dto.user.SystemUserLoginQry;
import com.xianyun.site.iblog.dto.user.data.SocialUserDTO;
import com.xianyun.site.iblog.dto.user.data.SystemUserDTO;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthUser;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

/**
 * @author mac_zyj
 */
@Slf4j
@Component
public class AuthUserLoginQueryExo {

    @Reference
    private SystemUserEchoService systemUserEchoService;

    @Reference
    private SocialUserEchoService socialUserEchoService;

    @Autowired
    private ISystemUserService systemUserService;

    @Autowired
    private RedisService redisService;


    public UserDetails execute(AuthUser authUser,String sessionId){
        saveAndUpdateAuthToken(authUser);
        SystemUserLoginQry qry=SystemUserLoginQry.builder()
                .socialId(authUser.getUuid())
                .source(authUser.getSource())
                .extendType(Integer.parseInt(LoginType.EXTEND.getCode())).build();
        // 1. 根据 三方登录的 唯一id 查找用户信息
        SystemUserDTO resultUser = systemUserEchoService.
                getUserInfoByConditionsForLogin(qry);
        // 2. 用户存在 --> 返回 UserDetails
        if (null != resultUser && StringUtils.isNotEmpty(resultUser.getUserName())) {
            return systemUserService.initAndQueryUserDetails(resultUser,LoginType.EXTEND);
        } else {
            String checkKey=sessionId+ AuthConstant.AUTH_LOGIN_BIND_CHECK;
            String securityKey = DigestUtils.md5DigestAsHex(checkKey.getBytes());
            redisService.setCacheObject(securityKey,qry);
            throw new JustAuthLoginException("三方登录失败,用户未绑定");
        }
    }
    private void  saveAndUpdateAuthToken(AuthUser authUser){
        SocialUserAddCmd socialUserAddCmd =new SocialUserAddCmd();
        SocialUserDTO socialUserDTO =new SocialUserDTO();
        BeanUtils.copyProperties(authUser,socialUserDTO);
        BeanUtils.copyProperties(authUser.getToken(),socialUserDTO,SocialUserDTO.class);
        socialUserAddCmd.setSocialUserDTO(socialUserDTO);
        Response response = socialUserEchoService.checkAndInsertAuthToken(socialUserAddCmd);
        if (!response.isSuccess()){
            //跳转到登录页
            throw new JustAuthLoginException(response.getErrMessage());
        }
    }

}
